<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="__JS__/jquery.js"></script>
    <link href="__CSS__/boot.css" rel="stylesheet">
    <link href="__STATIC__/layui/css/layui.css" rel="stylesheet">
    <script src="__JS__/layer/layer.js"></script>
    <script src="__STATIC__/layui/layui.js"></script>
</head>
<body>
<div style="height: 80px"></div>

<div style="width: 500px;margin: 0 auto">
    <label id="info" style="color: red;text-align: center;display: none" ></label>
    <form class="layui-form" action="" id="myform">
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;" >原密码</label>
            <div class="layui-input-block">
                <input type="password" name="pass" required  lay-verify="required" placeholder="原密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="newpass" required onblur="verificationNewPass()" lay-verify="required" placeholder="新密码由6-20位字母，数字，下划线组成" autocomplete="off" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label" style="width: 100px;">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" required onblur="verificationNewPassword()" lay-verify="required" placeholder="" autocomplete="off" class="layui-input" />
            </div>
        </div>
        <div class="layui-form-item layui-form-text" style="width: 300px;margin: 0 auto;">
            <div class="layui-input-block" >
                <button type="button" id="update" class="btn btn-primary">修改</button>
                <button type="button" id="cancel" class="btn btn-default">取消</button>
            </div>
        </div>
    </form>

</div>
<script>
    //判断新密码
    function verificationNewPass(){
        //新密码
        var password = $("input[name=newpass]").val();
        var patrn =/^(\w){6,20}$/;
        if(!password){
            $("#info").html('请输入密码');
            $("#info").css('display','block');
            return ;
        }
        if(!patrn.test(password)){
            $("#info").html('密码格式不正确');
            $("#info").css('display','block');
            return ;
        }
        $("#info").css('display','none');
    }
    //确认密码
    function verificationNewPassword() {
        var patrn =/^(\w){6,20}$/;
        //新密码
        var password = $("input[name=newpass]").val();
        //确认密码
        var pass = $("input[name=password]").val();
        if(pass != password){
            $("#info").html('两次密码不一致');
            $("#info").css('display','block');
            return ;
        }else{
            $("#info").css('display','none');
        }
        if(!patrn.test(pass)){
            $("#info").html('密码格式不正确');
            $("#info").css('display','block');
            return ;
        }
    }
    //取消
    $("#cancel").click(function () {
        //获取窗口索引
        var index = parent.layer.getFrameIndex(window.name);
        //关闭弹出的子页面窗口
        parent.layer.close(index);
    });
    //修改;
    $("#update").click(function () {
        //原密码
        var pass = $("input[name=pass]").val();
        if(!pass){
            $("#info").html('原密码不能为空');
            $("#info").css('display','block');
            return ;
        }
        //新密码
        var newpass = $("input[name=newpass]").val();
        if(!newpass){
            $("#info").html('新密码不能为空');
            $("#info").css('display','block');
            return ;
        }
        //确认密码
        var password = $("input[name=password]").val();
        if(!password){
            $("#info").html('确认密码不能为空');
            $("#info").css('display','block');
            return ;
        }
        $.ajax({
            url:"{:url('business/common/updatepass')}",
            data:$("#myform").serialize(),
            type:"post",
            dataType:"json",
            success:function (res) {
                if(res.error){
                    layer.alert(res.error);
                }
                if(res.success){
                    layer.alert(res.success,{bnt:['确定']},function () {
                        //获取窗口索引
                        var index = parent.layer.getFrameIndex(window.name);
                        //关闭弹出的子页面窗口
                        parent.layer.close(index);
                        //刷新
                        window.parent.location.reload();
                    })
                }
            }
        });
    });
</script>
</body>
</html>